<template>
  <div class="execution">
    <basic-container>
      <div class="table-container">
        <el-table
          border
          :header-cell-style="{ color: '#4D4D4D', background: '#fafafa' }"
          :data="list"
        >
          <el-table-column label="运费ID" min-width="100" align="center">
            <template slot-scope="scope">{{ scope.row.id }}</template>
          </el-table-column>
          <el-table-column label="模板名称" min-width="100" align="center">
            <template slot-scope="scope">{{ scope.row.name }}</template>
          </el-table-column>
          <el-table-column label="计费方式" align="center" min-width="120">
            <template slot-scope="scope">{{ scope.row.type | type }}</template>
          </el-table-column>
          <el-table-column label="编辑时间" align="center" min-width="120">
            <template slot-scope="scope">{{ scope.row.updateTime }}</template>
          </el-table-column>
          <el-table-column label="编辑人" align="center" min-width="120">
            <template slot-scope="scope">{{ scope.row.editName }}</template>
          </el-table-column>
          <el-table-column label="所属店铺" align="center" min-width="120">
            <template slot-scope="scope">{{ scope.row.storeName }}</template>
          </el-table-column>
          <el-table-column label="操作" min-width="200" align="center">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="seletDetail(scope.$index, scope.row)"
                >使用详情
              </el-button>
              <el-button
                type="text"
                @click="handleDelete(scope.$index, scope.row)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :page-size="listQuery.size"
          :page-sizes="[5, 10, 15]"
          :current-page.sync="listQuery.current"
          :total="total"
        >
        </el-pagination>
      </div>
    </basic-container>
  </div>
</template>
<script>
import "@/styles/index.scss";
import { fetchList, deleteFreight } from "@/api/pms/index";

export default {
  data() {
    return {
      list: [],
      listQuery: {
        size: 10,
        current: 1,
        storeId: null,
      },
      total: 0,
    };
  },
  created() {
    this.getList();
  },
  filters: {
    type(value) {
      if (value === 1) {
        return "包邮";
      } else if (value === 2) {
        return "按件计算";
      } else if (value === 3) {
        return "按重量计算";
      }
    },
  },
  methods: {
    getList() {
      fetchList(this.listQuery).then((res) => {
        if (res.data.code === 200) {
          this.list = res.data.data.records;
          this.total = res.data.data.total;
        } else {
          this.$message({
            message: res.data.msg,
            type: "warning",
            duration: 1000,
          });
        }
      });
    },
    seletDetail(index, row) {
      this.$router.push({
        path: "/pms/freight/details",
        query: { id: row.id },
      });
    },
    handleSizeChange(val) {
      this.passListQuery.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.passListQuery.current = val;
      this.getList();
    },
    handleDelete(index, row) {
      this.$confirm("是否要删除运费模板", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteFreight(row.id).then((response) => {
          if (response.data.code === 200) {
            this.$message({
              message: res.data.msg,
              type: "success",
              duration: 1000,
            });
            this.getList();
          } else {
            this.$message({
              message: res.data.msg,
              type: "warning",
              duration: 1000,
            });
          }
        });
      });
    },
  },
};
</script>
